﻿using System;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using NewsPortal.DataContracts;
using NewsPortal.Linq;
using NewsPortal.Models;

namespace NewsPortal.Controllers
{
    public class PortalController : Controller
    {
        [OutputCache(CacheProfile="MyCache",VaryByParam = "None")]
        public ActionResult Run()
        {
            var portalModel = new PortalModel();
            using (var connection = DataContextFactory.Create())
            {
                var categories =
                     from category in connection.GetTable<Category>()
                     orderby category.Priority
                     select category;
                portalModel.AddCategores(categories.ToList());
                var cat = (Category) categories.Take(1).ToArray()[0];
                var guid = cat.Id;
                var newses =
                        from news in connection.GetTable<News>()
                        orderby news.Moment descending
                        where news.CategoryId == guid
                        select news;
                portalModel.AddNews(categories.Take(1).ToArray()[0], newses.Take(10).ToArray());
            }
            return View("PortalView", portalModel);
        }
    }
}
